
<?php
session_start();
ob_start();
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet"type="text/css"href="css/css.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <title>Hieu</title>
    </head>


    <body id="khung">
        <div id="banner"></div>

        <div id="menu">
            <ul id="a">
                <li><a href="#">Trang Chu</a>  </li>
                <li><a href="#">Manga</a> </li>
                <li><a href="#">Music</a> </li>
                <li><a href="#">Haivl</a> </li>

            </ul>
        </div>


        <?php
        if (isset($_GET["logout"]) && $_GET["logout"] == true) {
            unset($_SESSION["id"]);
            header("location:login.php");
        }
        
        ?>

        
        <div id="page_body">       
            <div id="h1">
                <h1 align="center">Hu Tieu</h1> 
            </div>



            <?php if (isset($_SESSION['id'])): ?>
                <a href="index.php?logout=true"><h3>Logout</h3></a>
            <?php else: ?>
                <a href="login.php"><h3>Login</h3></a>
            <?php endif; ?>

                 <div class="ajax_show"></div>
            <?php
            require_once 'model/db.php';
            require_once 'model/keywords.php';
            require_once 'model/works.php';


            $works = new works();
            ?>
            <br>
            <?php $page = $works->count() / PAGE; ?>

            <?php if (!isset($_GET['page'])): ?>
                <a href='index.php?page=<?php echo $_GET['page'] = 1; ?>'></a>
            <?php endif; ?>


            <a href='index.php?page=1'> Trang Dau...  </a>
            <a href='index.php?page=<?php echo $_GET["page"] - 1 ?>'> Truoc....  </a>  

            <?php for ($i = 1; $i <= 10; $i++): ?>
                <?php if (($_GET["page"] == 1) || ($_GET["page"] == 2) || ($_GET["page"] == 3) || ($_GET["page"] == 4) || ($_GET["page"] == 5)): ?>            
                    <?php for ($i = 1; $i <= 10; $i++): ?>
                        <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a>
                        <?php if ($_GET["page"] == $i): ?>
                            <?php $works->start = (($i - 1) * 10); ?>
                            <?php $works->end = PAGE; ?>
                        <?php endif; ?>
                    <?php endfor; ?>

                <?php elseif (($_GET["page"] == $page) || ($_GET["page"] == $page - 1) || ($_GET["page"] == $page - 2) || ($_GET["page"] == $page - 3) || ($_GET["page"] == $page - 4)): ?>        
                    <?php for ($i = $page - 10; $i <= $page; $i++): ?>
                        <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a>
                        <?php if ($_GET["page"] == $i): ?>
                            <?php $works->start = (($i - 1) * 10); ?>
                            <?php $works->end = PAGE; ?>
                        <?php endif; ?>
                    <?php endfor; ?>

                <?php else : ?>
                    <?php for ($i = $_GET["page"] - 4; $i <= $_GET["page"] + 4; $i++): ?>
                        <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a>
                        <?php if ($_GET["page"] == $i): ?>
                            <?php $works->start = (($i - 1) * 10); ?>
                            <?php $works->end = PAGE; ?>
                        <?php endif; ?>
                    <?php endfor; ?>
                <?php endif; ?> 
            <?php endfor; ?>   

            <a href='index.php?page=<?php echo $_GET["page"] + 1 ?>'>.... Tiep </a>
            <a href='index.php?page=<?php echo $page; ?>'> ....Trang Cuoi </a>

            <?php $work = $works->bbb(); ?>

            <table border="1px">
                <tr>
                    <td>id </td>
                    <td>name </td>
                    <td>location</td>
                    <td>company_address</td>  
                    <td>description</td>
                    <td>image </td> 

                    <td>Up</td> 


                </tr>

                <?php foreach ($work as $works): ?>

                    <tr>
                        <td><?php echo $works["id"]; ?></td>
                        <td><?php echo $works["name"]; ?></td>
                        <td><?php echo $works["location"]; ?></td>
                        <td><?php echo $works["company_address"]; ?></td> 
                        <td><?php echo substr($works["description"], 0, 70); ?></td>
                        <td><img src="<?php echo $works['image']; ?>" width="200px" height="100"></td>
                        <?php if (isset($_SESSION['id']) && $_SESSION['id'] == 0) : ?>

                            <td>
                                <a href="http://localhost:82/trunghieu-web2/upload.php?id=<?php echo $works["id"]; ?>"> Edit</a>
                                <a href="http://localhost:82/trunghieu-web2/view.php?id=<?php echo $works["id"]; ?>"> View</a>
                                 <a href="#" class="ajax_view" id_works="<?php echo $works['name']; ?>"><?php echo "View" ?></a><br>
                                <a href='data/picture/'><?php echo "Delete" ?></a>
                            </td>
                        <?php else: ?>
                            <td>
                                <a href="#" class="ajax_view" id_works="<?php echo $works['name']; ?>"><?php echo "View" ?></a><br>
                            </td>

                        <?php endif; ?>
                    </tr>

                <?php endforeach; ?>

            </table>

        </div>
        <br>
        <script type="text/javascript" >
            $(document).ready(function() {
                $(".ajax_view").click(function() {
                    id_works = $(this).attr('id_works');
                    url = "ajax.php?id_works=" + id_works;
                    $.ajax({
                        url: url
                    }).done(function(data) {
                        $('.ajax_show').html(data);
                    });
                });
            });
        </script>
        <div id="footer">
            <div id="h1">
                <h1 align="center">Hieu Ban Hu Tieu - C12TDT </h1>
            </div>
        </div>
    </body>
</html>
<?php ob_end_flush(); ?>